//////////////////\\ddd// pages/car/car.js
const app = getApp()
Page({
  data: {
    isAllSelect: false,
    totalMoney: 0,
    num: 1,
    price:0,
    car_list:'',
    prices:0.01,
    text: "管理",
    isManage: true,
    car_delete: "none",
    shop_goods:'',//暂时只做一个商铺的商品
    // 商品详情介绍
    carts: [
      {
        pic: "../image/order_list_01_03.png",
        name: "2018年最新村“两委”换届啊空间风格较大付款党换届啊空间风格较换届啊空间风格较大付款党",
        price: 89.90,
        isSelect: false,
        num: 1,
        // 数据设定
        count: {
          quantity: 1,
          min: 1,
          max: 20
        },
      },
    ],
  },
  //购物车管理
  manage: function () {
    if (this.data.isManage == false) {
      this.setData({
        text: "管理",
        isManage: true
      })
    }
    else {
      this.setData({
        text: "完成",
        isManage: false
      })
    }
  },
  // 增加数量
  addCount(e) {
    let that = this;
    var car_list = that.data.car_list;      //获取商品列表
    var id = e.currentTarget.dataset.id;
    car_list.forEach(function (list, i) {
      list.goods.forEach(function (listdata, v) {
        if (listdata.cartid == id) {
          listdata.num = listdata.num + 1
          wx.request({
            url: app.data.Url + 'cart/edit',
            data: {
              cartid: id,
              type: 'plus'
            },
            method: 'post',
            success: function (res) {
              console.log(res)
            }
          })
        }
      })
    })
    that.setData({
      car_list: car_list
    })
    that.price()
  },
  // 减少数量
  minusCount(e) {
    let that = this;
    var car_list = that.data.car_list;      //获取商品列表
    var id = e.currentTarget.dataset.id;
    car_list.forEach(function (list, i) {
      list.goods.forEach(function (listdata, v) {
        if (listdata.cartid == id) {
          if (listdata.num <= 1){
            return false;
          }else{
            listdata.num = listdata.num - 1
            wx.request({
              url: app.data.Url + 'cart/edit',
              data: {
                cartid: id,
                type: 'cut'
              },
              method: 'post',
              success: function (res) {
                console.log(res)
              }
            })
          }
          
        }
      })
    })
    that.setData({
      car_list: car_list
    })
    that.price()
  },

  //选择  已改 0718 @ yb
  switchSelect:function(e){
    let that = this;
    var id = e.currentTarget.dataset.id;      //获取商品id
    var car_list = that.data.car_list;          //获取购物车列表
    car_list.forEach(function (list, i) {
      list.goods.forEach(function (listdata, v) {
        if (listdata.cartid == id){
          listdata.status = !listdata.status
        }
      })
    })
    that.setData({
      car_list: car_list
    })
    that.price()
  },
  //删除
  del:function(){
    let that = this;
    var car_list = that.data.car_list;      //获取商品列表  
    var cartid = [];
    car_list.forEach(function (list, i) {
      list.goods.forEach(function (listdata, v) {
        if (listdata.status) {
          cartid += listdata.cartid+','
          //list.goods.splice(v,1)
        }
      })
    })
    console.log(cartid,'cartidcartid')
    if (cartid.length<0){
      wx.showToast({
        title: '请选择商品',
      })
    }else{
      wx.request({
        url: app.data.Url + 'Cart/delete',
        data: {
          cartid: cartid,
          uid: wx.getStorageSync('userInfo').uid
        },
        method: 'get',
        success: function (res) {
          if(res.data.code != 0){
            that.setData({
              car_list: res.data.data
            })
          }else{
            wx.showToast({
              title: '请选择商品',
            })
          }
          
        }
      })
      that.price()
    }
  },


  //全选
  allSelect: function (e) {
    let that = this;
    var car_list = that.data.car_list;      //获取商品列表 
    if (!that.data.isAllSelect) {
      car_list.forEach(function (list, i) {
        list.goods.forEach(function (listdata, v) {
          listdata.status = true
        })
      })
    }else{
      car_list.forEach(function (list, i) {
        list.goods.forEach(function (listdata, v) {
          listdata.status = false
        })
      })
    }
    that.setData({
      car_list: car_list,
      isAllSelect: !that.data.isAllSelect
    })
    that.price()
  },



  toFix: function (value) {
    return value.toFixed(2)//此处2为保留两位小数
  },
  

  //总价 已改 0718 @ yb
  price:function(){  //计算价格
    let that = this;
    var price = 0;
    var car_list = that.data.car_list;      //获取商品列表  
    var cartid = [];
    car_list.forEach(function (list, i) {
      list.goods.forEach(function (listdata, v) {
        if (listdata.status){
          cartid.push(listdata.cartid)
          price += listdata.min_price * listdata.num
        }
      })
    })
    that.setData({
      price: price.toFixed(2),
      cartid: cartid
    })
    //console.log(that.data.cartid,'cartid')
  },
  // 去结算
  toBuy() {
    wx.showToast({
      title: '去结算',
      icon: 'success',
      duration: 3000
    }); wx.showToast({
      title: '收藏成功！',
    })
    this.setData({
      showDialog: !this.data.showDialog
    });
  },
  //数量变化处理
  handleQuantityChange(e) {
    var componentId = e.componentId;
    //console.log(componentId)
    var quantity = e.quantity;
    this.data.carts[componentId].count.quantity = quantity;
    this.setData({
      carts: this.data.carts,
    });
  },

  //收藏
  collection:function(){
    let that = this;
    var cartid = '';
    var car_list = that.data.car_list;      //获取商品列表  
    car_list.forEach(function (list, i) {
      list.goods.forEach(function (listdata, v) {
        if (listdata.status) {
          cartid += listdata.goodsid+','
          //list.goods.splice(v,1)
        }
      })
    })
    if (cartid == '' || cartid == null){
      wx.showToast({
        title: '请选择商品',
        icon: 'success',
        duration: 1000
      })
    }else{
      wx.request({
        url: app.data.Url + 'Collect/addscollect',
        data: {
          goodsid: cartid,
          uid: wx.getStorageSync('userInfo').uid,
          type: 0
        },
        method: 'post',
        success: function (res) {
          wx.showToast({
            title: '收藏成功',
            icon: 'success',
            duration: 1000
          })
          // that.setData({
          //   car_list: res.data.data
          // })
        }
      })
    }
   
  },


  to_detail(e){
    //console.log(e)
    var id = e.currentTarget.dataset.goodsid
    // wx.navigateTo({
    //   url: '../normal/normal?id='+id,
    // })
  },
  onLoad:function(){
    console.log('onLoad')
    // let that=this;
    // wx.request({
    //   url: 'https://zhapp.4hl.cn/api/cart/cartLists',  // app.data.Url + 'cart/cartLists',
    //   data:{
    //     // uid:wx.getStorageSync('userInfo').uid,
    //     uid:1387
    //   },
    //   method:'get',
    //   success:function(res){
    //     //console.log(res.data.data[0].goods)
    //     var carts = res.data.data;
    //     //console.log(carts[0].goods.length)
    //     for (var i = 0; i <= carts[0].goods.length-1; i++) {
    //       //console.log(i)
    //       carts[0].goods[i].min = 0
    //     }
    //     that.setData({
    //       carts: carts
    //     })
    //     console.log(res.data.data)
    //   }
    // })
  },
  onShow:function(){
    let that = this;
    that.setData({
      isManage:true,
      isAllSelect:false,
      price:0,
      text:'管理'
    })
    wx.request({
      url: 'https://zhapp.4hl.cn/api/cart/cartLists',//app.data.Url + 'cart/cartLists',
      data: {
         uid: wx.getStorageSync('userInfo').uid
        //uid: 1387
      },
      method: 'get',   
      success: function (res) {
        var car_list = []
        car_list = res.data.data
        car_list.forEach(function(list,i){
          list.goods.forEach(function (listdata,v){
            listdata.status = false;
          })
        })
        //console.log(car_list,'渲染完成')
        that.setData({
          car_list: car_list,
        })
      
      }
    })
  },
  jiesuan:function(){
    let that=this;
    // var car_list = that.data.car_list;          //获取购物车列表
    // car_list.forEach(function (list, i) {
    //   list.goods.forEach(function (listdata, v) {
    //     if (listdata.cartid == id) {
    //       listdata.status = !listdata.status
    //     }
    //   })
    // })
    if (that.data.cartid == '' || that.data.cartid==undefined){
      wx.showToast({
        title: '请选择商品',
      })
    }else{
      wx.navigateTo({
        url: '../confirms/confirm?money=' + that.data.price + '&cartid=' + that.data.cartid,
      })
    }
  },
  onurl:function(e){
    let that = this;
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../normal/normal?id='+id,
    })
  },
  onHide:function(){
    this.setData({
      car_list: '',
    })
  }
});
